package com.example.market.service;

import com.example.market.exception.ExceptionEnum;
import com.example.market.exception.ServiceException;
import com.example.market.mapper.ProductPictureMapper;
import com.example.market.pojo.ProductPicture;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductPictureService {


    private ProductPictureMapper productPictureMapper;

    @Autowired
    public void setProductPictureMapper(ProductPictureMapper productPictureMapper) {
        this.productPictureMapper = productPictureMapper;
    }

    public List<ProductPicture> getProductPictureByProductId(String productId) {
        System.out.println(
                productId
        );
        ProductPicture picture = new ProductPicture();
        picture.setProductId(Integer.parseInt(productId));
        List<ProductPicture> list;
        try {
            list = productPictureMapper.select(picture);
            if (ArrayUtils.isEmpty(list.toArray())) {
                throw new ServiceException(ExceptionEnum.GET_PRODUCT_PICTURE_NOT_FOUND);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException(ExceptionEnum.GET_PRODUCT_PICTURE_ERROR);
        }
        return list;
    }
}
